package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.Menu;
import com.example.yiqinmanagersystem.entity.Role;
import com.example.yiqinmanagersystem.service.MenuService;
import com.example.yiqinmanagersystem.service.RoleService;
import com.example.yiqinmanagersystem.utils.TreeNode;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import com.example.yiqinmanagersystem.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 **@Author:youpeng
 **Created on 2022/9/16
 */
@RestController
@RequestMapping("/api")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    /*
    * 获取角色列表, 带分页以及查询条件
    * */
    @GetMapping("/role/loadAllRole")
    public ResponseResult loadAllRole(RoleVo roleVo){
        Page<Role> page = new Page<>(roleVo.getPage(),roleVo.getLimit());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(roleVo.getName()),"name",roleVo.getName());
        queryWrapper.like(StringUtils.isNoneBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        Page<Role> rolePage = roleService.page(page, queryWrapper);
       if(rolePage!=null){
           return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(page.getTotal()),page.getRecords());
       }else {
           return new ResponseResult(ResponseCode.FAILED);
       }
    }

    /*
    * 添加角色
    * */
    @PostMapping("/role/addRole")
    public ResponseResult addRole(Role role){
        boolean b = roleService.save(role);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     * 删除角色
     * */
    @PostMapping("/role/deleteRole")
    public ResponseResult deleteRole(Role role){
        boolean b = roleService.removeById(role.getId());
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     * 修改 (添加)角色
     * */
    @PostMapping("/role/updateRole")
    public ResponseResult updateRole(Role role){
        boolean b = roleService.saveOrUpdate(role);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 初始化下拉列表的具有权限
    * */
    @PostMapping("/role/initPermissionByRoleId")
    public ResponseResult initPermissionByRoleId(Integer roleId){
        //1.把所有的菜单权限查出来
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        List<Menu> list = menuService.list();
        //2.根据角色 id【roleId】查询所有的菜单 mid
        List<Integer> currentRolePermissions = roleService.queryAllPermissionByRid(roleId);
        //3. rid mid 所有的id 查询菜单和角色的数据
        List<Menu> menus = null;
        //4.查询到 mid rid
        if(currentRolePermissions.size()>0){
            queryWrapper.in("id",currentRolePermissions);
            menus = menuService.list(queryWrapper);
        }else {
            menus = new ArrayList<>();
        }
        //5.返回前端格式 带层级关系
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu allPermission : list) {
            String checkArr = "0";
            for (Menu currentPermission : menus) {
                if(allPermission.getId().equals(currentPermission.getId())){
                    checkArr = "1";
                    break;
                }
            }
            Boolean spread = (allPermission.getOpen() == null || allPermission.getOpen() == 1) ? true : false;
            nodes.add(new TreeNode(allPermission.getId(),allPermission.getPid(),allPermission.getTitle(),spread,checkArr));
        }
        if(nodes!=null){
            return new ResponseResult(ResponseCode.SUCCESS,nodes);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 点击分配权限
    * 插入数据库表
    * */
    @PostMapping("/role/saveRolePermission")
    public ResponseResult saveRolePermission(Integer rid, Integer[] mids){
        //1.根据 rid 删除之前所有的mid权限
        roleService.deleteRoleByRid(rid);
        if(mids!=null && mids.length>0){
            for (Integer mid : mids) {
                roleService.saveRoleMenu(rid,mid);
            }
        }
        return new ResponseResult(ResponseCode.SUCCESS);
    }

}
